import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter01/pages/getx/count_controller.dart';
import 'package:get/get.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'other_Page.dart';

class GetXIndex extends StatelessWidget {
  const GetXIndex({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final countController = Get.put(CountController());

    // SharedPreferences.getInstance().then((prefs){
    //   final int? counter = prefs.getInt('getXCounter') ;
    //   if(counter != null){
    //     //做个定时 方便观察数据变化
    //     Timer(Duration(seconds: 3),(){
    //       countController.count.value = counter;
    //     });
    //   }
    // });

    final int? counter = Get.find<SharedPreferences>().getInt('getXCounter');
    if (counter != null) {
      //做个定时 方便观察数据变化
      Timer(Duration(seconds: 3), () {
        countController.count.value = counter;
      });
    }
    ;

    return Scaffold(
      appBar: AppBar(title: Text('Getx')),
      body: Center(
          child: Column(children: [
        ElevatedButton(
            onPressed: () {
              Get.snackbar("snack title", "welcome snack bar");
            },
            child: Text("显示 SnackBar")),
        ElevatedButton(
            onPressed: () {
              Get.defaultDialog(
                title: 'Dialog',
                content: Text('欢迎使用Dialog'),
                onConfirm: () {},
                textConfirm: '确定',
                textCancel: '取消',
              );
            },
            child: Text('显示 Dialog')),
        ElevatedButton(
            onPressed: () {
              Get.bottomSheet(
                  Container(
                      child: Wrap(
                    children: [
                      ListTile(
                          leading: Icon(Icons.wb_sunny_outlined),
                          title: Text('白天模式'),
                          onTap: () {
                            Get.changeTheme(ThemeData.light());
                          }),
                      Divider(color: Colors.grey),
                      ListTile(
                          leading: Icon(Icons.wb_sunny),
                          title: Text('黑夜模式'),
                          onTap: () {
                            Get.changeTheme(ThemeData.dark());
                          })
                    ],
                  )),
                  barrierColor: Colors.transparent);
            },
            child: Text("显示 BottomSheet")),
        ElevatedButton(
            onPressed: () {
              Get.toNamed("/dio");
            },
            child: Text('GetX路由跳转')),
        ElevatedButton(
            onPressed: () {
              countController.increment();
            },
            child: Obx(() => Text('count:${countController.count}'))),
        ElevatedButton(
            onPressed: () {
              Get.to(OtherPage(),
                  transition: Transition.rightToLeftWithFade,
                  arguments: "getx page jump to ohter page");
            },
            child: Text('Go to Other')),
        Divider(),
        Text(Get.find<SharedPreferences>().getString('rootStr') ?? 'null'),
        Divider(),
        Text(Get.find<String>(tag: "globalString")),
      ])),
    );
  }
}
